<template>
  <div>
    <!-- 页面基础模板 -->
    <base-header-footer
      :isShowContentTitle="false"
      :isShowBreadCrumb="false"
      :isShowArchived="true"
    >
      <!-- isShowTitle : 布尔值，是否显示当前页面标题组件-->
      <!-- isShowBreadCrumb : 布尔值，是否显示当前页面面包屑导航组件-->
      <!-- isShowArchived : 布尔值，是否显示当前页面归档组件-->
      <template slot="content-item-breadCrumb">
        <!-- 自定义面包屑导航插槽 -->
      </template>
      <template slot="content-item">
        <!-- 内容插槽 -->
        <div class="personal">
          <div class="personal-bg">
            <img
              :src="
                userInfo.bgCover
                  ? userInfo.imgHost + userInfo.bgCover
                  : require('@/static/personal-bg.jpg')
              "
              alt=""
            />
          </div>
          <div class="personal-content">
            <div class="upload-bg" @click="dialogVisible = true">
              <span><i class="el-icon-camera-solid"></i>修改封面</span>
            </div>
            <UploadAvatar></UploadAvatar>
            <p>
              <span
                ><span>{{ articleListTotalNum }}</span
                >文章</span
              >
              <span
                ><span>{{ commentListTotalNum }}</span
                >评论</span
              >
              <span><span>0</span>粉丝</span>
            </p>
            <div>
              <button><i class="el-icon-plus"></i>关注</button>
              <button><i class="el-icon-message"></i>私信</button>
            </div>
          </div>
        </div>
        <el-dialog
          title="选择图片"
          :visible.sync="dialogVisible"
          :modal-append-to-body="false"
          :close-on-click-modal="false"
        >
          <div class="upload-dialog-item" v-if="!isShowCropper">
            <i class="iconfont icon-xiangji"></i>
            <p>请选择背景图片</p>
            <el-button type="primary" @click="handleOpenFile"
              >选择图片</el-button
            >
            <input
              ref="uploadFile"
              type="file"
              name=""
              id="uploadFile"
              accept="image/*"
              v-show="false"
              @change="handleChangeFile"
            />
          </div>
          <div class="cropper-content" v-else>
            <div class="cropper-box">
              <div class="cropper">
                <vue-cropper
                  ref="cropper"
                  :img="option.img"
                  :outputSize="option.outputSize"
                  :outputType="option.outputType"
                  :info="option.info"
                  :canScale="option.canScale"
                  :autoCrop="option.autoCrop"
                  :autoCropWidth="option.autoCropWidth"
                  :autoCropHeight="option.autoCropHeight"
                  :fixed="option.fixed"
                  :fixedNumber="option.fixedNumber"
                  :full="option.full"
                  :fixedBox="option.fixedBox"
                  :canMove="option.canMove"
                  :canMoveBox="option.canMoveBox"
                  :original="option.original"
                  :centerBox="option.centerBox"
                  :height="option.height"
                  :infoTrue="option.infoTrue"
                  :maxImgSize="option.maxImgSize"
                  :enlarge="option.enlarge"
                  :mode="option.mode"
                >
                </vue-cropper>
              </div>
            </div>
            <div class="close-cropper" @click="isShowCropper = false">
              <i class="el-icon-close"></i>
            </div>
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="handleSubmit">应用</el-button>
            <el-button @click="handleClose">取消</el-button>
          </span>
        </el-dialog>
        <div class="personal-tabs">
          <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
            <el-tab-pane label="文章" name="first">
              <div class="tabs-article" v-if="articleList.length == 0">
                <img src="@/static/empty-post.svg" alt="" />
                <p>你还没有发布任何文章。</p>
              </div>
              <div v-else>
                <UserArticleList :articleList="articleList"></UserArticleList>
                <div class="toLoad" v-show="isShowToLoad">
                  <p @click="handleToLoad">点击查看更多</p>
                </div>
              </div>
            </el-tab-pane>
            <el-tab-pane label="评论" name="second">
              <div class="tabs-article" v-if="commentList.length == 0">
                <img src="@/static/empty-post.svg" alt="" />
                <p>你还没有发布任何评论。</p>
              </div>
              <div v-else>
                <UserCommentList :commentList="commentList"></UserCommentList>
                <div class="toLoad" v-show="isShowToLoad">
                  <p @click="handleToLoad">点击查看更多</p>
                </div>
              </div>
            </el-tab-pane>
            <el-tab-pane label="问答" name="third">
              <div class="tabs-btn">
                <button
                  :class="activeBtnOne ? 'active' : ''"
                  @click="handleQa(true)"
                >
                  问题
                </button>
                <button
                  :class="!activeBtnOne ? 'active' : ''"
                  @click="handleQa(false)"
                >
                  回答
                </button>
              </div>
              <div class="tabs-article">
                <img src="@/static/empty-qa.svg" alt="" />
                <p>你还没有{{ activeBtnOne ? "发布" : "回答" }}任何问题。</p>
              </div>
            </el-tab-pane>
            <el-tab-pane label="关注" name="fourth">
              <div class="tabs-btn">
                <button
                  :class="activeBtnOne ? 'active' : ''"
                  @click="handleQa(true)"
                >
                  Ta关注的人
                </button>
                <button
                  :class="!activeBtnOne ? 'active' : ''"
                  @click="handleQa(false)"
                >
                  关注Ta的人
                </button>
              </div>
              <div class="tabs-article">
                <img src="@/static/empty-follow.svg" alt="" />
                <p v-show="activeBtnOne">您还没有关注任何用户。</p>
                <p v-show="!activeBtnOne">您还没有被任何粉丝关注。</p>
              </div></el-tab-pane
            >
            <el-tab-pane label="收藏" name="five">
              <div class="tabs-article" v-if="collectList.length == 0">
                <img src="@/static/empty-favorite.svg" alt="" />
                <p>你还没有收藏的文章</p>
              </div>
              <div v-else>
                <UserArticleList :articleList="collectList"></UserArticleList>
                <div class="toLoad" v-show="isShowToLoad">
                  <p @click="handleToLoad">点击查看更多</p>
                </div>
              </div>
            </el-tab-pane>
          </el-tabs>
        </div>
      </template>
    </base-header-footer>
  </div>
</template>

<script>
// import { VueCropper } from "vue-cropper";
import UserArticleList from "@/components/otherUser/ArticleList.vue";
import UserCommentList from "@/components/otherUser/CommentList.vue";
import UploadAvatar from "@/components/userlist/uploadAvatar/UploadAvatar.vue";
import BaseHeaderFooter from "@/components/baseheaderfooter/BaseHeaderFooter.vue";
// import {
//   getArticle,
//   getOtherComment,
//   getOtherCollect,
//   uploadAvatar,
// } from "@/api/api";
import { mapState, mapMutations } from "vuex";
export default {
  name: "",
  components: {
    // VueCropper,
    UserArticleList,
    UserCommentList,
    UploadAvatar,
    BaseHeaderFooter,
  },
  data() {
    return {
      dialogVisible: false,
      isShowCropper: false,
      isShowToLoad: true,
      activeName: "",
      activeBtnOne: true,
      OtherUserInfo: {},
      articleList: [],
      articleListTotalNum: 0,
      commentList: [],
      commentListTotalNum: 0,
      collectList: [],
      totalPage: 0,
      currentPage: 1,
      option: {
        img: "", //裁剪图片的地址
        outputSize: 1, //裁剪生成图片的质量(可选0.1 - 1)
        outputType: "jpeg", //裁剪生成图片的格式（jpeg || png || webp）
        info: true, //图片大小信息
        canScale: true, //图片是否允许滚轮缩放
        autoCrop: true, //是否默认生成截图框
        autoCropWidth: 810, //默认生成截图框宽度
        autoCropHeight: 300, //默认生成截图框高度
        fixed: true, //是否开启截图框宽高固定比例
        fixedNumber: [810, 300], //截图框的宽高比例
        full: false, //false按原比例裁切图片，不失真
        fixedBox: true, //固定截图框大小，不允许改变
        canMove: true, //上传图片是否可以移动
        canMoveBox: true, //截图框能否拖动
        original: false, //上传图片按照原始比例渲染
        centerBox: true, //截图框是否被限制在图片里面
        height: true, //是否按照设备的dpr 输出等比例图片
        infoTrue: false, //true为展示真实输出图片宽高，false展示看到的截图框宽高
        // maxImgSize: 3000, //限制图片最大宽度和高度
        enlarge: 1, //图片根据截图框输出比例倍数
        // mode: "230px 150px", //图片默认渲染方式
      },
    };
  },
  computed: {
    ...mapState(["userInfo"]),
  },
  created() {
    this.getArticleInfo();
    this.getCommentInfo();
    this.getCollectInfo();
    this.activeName = this.$cookies.get("activeName") || "first";
  },
  methods: {
    ...mapMutations(["setUserInfo"]),
    async getArticleInfo() {
      const res = await this.$api.getArticle({
        currentPage: this.currentPage,
        num: 10,
        otherUserId: this.userInfo.id,
      });
      this.articleList = [...this.articleList, ...res.data];
      this.totalPage = res.totalPage;
      this.articleListTotalNum = res.totalNum;
      // console.log(res.totalPage);
      if (
        (this.currentPage == this.totalPage && this.activeName == "first") ||
        res.totalPage == 0
      ) {
        this.isShowToLoad = false;
      }
    },
    async getCommentInfo() {
      const res = await this.$api.getOtherComment({
        currentPage: this.currentPage,
        num: 10,
        otherUserId: this.userInfo.id,
      });
      this.commentList = [...this.commentList, ...res.data];
      this.totalPage = res.totalPage;
      this.commentListTotalNum = res.totalNum;
      // console.log(res.totalPage);
      if (
        (this.currentPage == this.totalPage && this.activeName == "second") ||
        res.totalPage == 0
      ) {
        this.isShowToLoad = false;
      }
    },
    async getCollectInfo() {
      const res = await this.$api.getOtherCollect({
        currentPage: this.currentPage,
        num: 10,
        otherUserId: this.userInfo.id,
      });
      this.collectList = [...this.collectList, ...res.data];
      this.totalPage = res.totalPage;
      if (
        (this.currentPage == this.totalPage && this.activeName == "five") ||
        res.totalPage == 0
      ) {
        this.isShowToLoad = false;
      }
    },
    handleOpenFile() {
      this.$refs.uploadFile.click();
    },
    handleChangeFile(e) {
      this.isShowCropper = true;
      let file = e.target.files[0];
      if (!/\.(jpg|jpeg|png|JPG|PNG)$/.test(e.target.value)) {
        this.$message({
          message: "图片类型要求：jpeg、jpg、png",
          type: "error",
        });
        return false;
      }
      //转化为blob
      let reader = new FileReader();
      reader.onload = (e) => {
        let data;
        if (typeof e.target.result === "object") {
          data = window.URL.createObjectURL(new Blob([e.target.result]));
        } else {
          data = e.target.result;
        }
        this.option.img = data;
      };
      //转化为base64
      reader.readAsDataURL(file);
    },
    handleSubmit() {
      this.$refs.cropper.getCropBlob(async (data) => {
        // do something
        // console.log(data);
        // console.log(this.$refs.uploadFile.files[0]);
        let files = new File([data], "bgCover.jpg");
        let formData = new FormData();
        formData.append("id", this.userInfo.id);
        formData.append("file", files);
        const res = await this.$api.uploadAvatar(formData);
        // console.log(res);
        this.userInfo.bgCover = res.data.bgCover;
        this.setUserInfo({value:this.userInfo,app:this});
        this.dialogVisible = false;
        this.isShowCropper = false;
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.isShowCropper = false;
    },
    handleClick(tab, event) {
      this.currentPage = 1;
      this.totalPage = 0;
      this.isShowToLoad = true;
      this.articleList = [];
      this.commentList = [];
      this.collectList = [];
      if (this.activeName == "first") {
        this.getArticleInfo();
      } else if (this.activeName == "second") {
        this.getCommentInfo();
      } else if (this.activeName == "five") {
        this.getCollectInfo();
      }
      this.activeBtnOne = true;
      this.$cookies.set("activeName", this.activeName);
    },
    handleQa(value) {
      this.activeBtnOne = value;
    },
    handleToLoad() {
      this.currentPage++;
      if (this.activeName == "first") {
        this.getArticleInfo();
      } else if (this.activeName == "second") {
        this.getCommentInfo();
      } else if (this.activeName == "five") {
        this.getCollectInfo();
      }
    },
  },
};
</script>
<style lang="less" scoped>
@media only screen and (min-width: 992px) and (max-width: 1240px) {
  .personal {
    .personal-content {
      padding: 0 !important;
    }
  }
}
@media only screen and (max-width: 992px) {
  .personal {
    .personal-content {
      padding: 10px !important;
    }
  }
}
@media only screen and (max-width: 768px) {
  /deep/.content {
    margin-top: 0 !important;
  }
  .personal {
    border: 0 !important;
    .personal-bg {
      position: initial !important;
    }
    .personal-content {
      color: #000 !important;
      margin-top: -50px;
      background: #fff !important;
      &:hover {
        .upload-bg {
          display: none !important;
        }
      }
    }
  }
  .personal-tabs {
    border: 0 !important;
  }
}
.personal {
  border: 10px solid #f5f5f5;
  position: relative;
  .personal-bg {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    z-index: -1;
    img {
      width: 100%;
    }
  }
  .personal-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    color: #fff;
    padding: 35px 10px;
    background: rgba(0, 0, 0, 0.3);
    p {
      margin-bottom: 20px;
      font-size: 16px;
      line-height: 1.8;
      span {
        padding: 0 8px;
        span {
          padding: 0 4px;
          font-weight: bold;
        }
      }
    }
    > div {
      button {
        color: #fff;
        margin: 0 8px;
        padding: 6px 15px;
        border-radius: 5px;
        background: #08c;
        border: 0;
        i {
          margin-right: 3px;
        }
      }
    }
    &:hover {
      .upload-bg {
        display: block;
      }
    }
    .upload-bg {
      display: none;
      position: absolute;
      top: 15px;
      right: 15px;
      font-size: 12px;
      color: #fff;
      border: 1px solid #fff;
      padding: 5px 10px;
      border-radius: 3px;
      cursor: pointer;
      i {
        margin-right: 3px;
      }
      &:hover {
        border: 1px solid #3361ea;
        background: #3361ea;
      }
    }
  }
}

/deep/.el-dialog {
  width: initial;
  max-width: 890px;
  text-align: initial;
  .el-dialog__body {
    padding: 0;
    .upload-dialog-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 30px 20px;
      i {
        font-size: 50px;
      }
      p {
        font-size: 16px;
        margin: 5px auto 20px;
      }
    }
    .cropper-content {
      position: relative;
      display: flex;
      display: -webkit-flex;
      justify-content: flex-end;
      .cropper-box {
        flex: 1;
        width: 100%;
        .cropper {
          width: auto;
          height: 500px;
        }
      }
      .close-cropper {
        position: absolute;
        top: 5px;
        right: 5px;
        width: 24px;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 24px;
        font-size: 16px;
        border-radius: 50%;
        color: #fff;
        background: rgba(0, 0, 0, 0.3);
        cursor: pointer;
      }
    }
  }
}
.personal-tabs {
  border: 10px solid #f5f5f5;
  background: #fff;
  /deep/.el-tabs {
    .el-tabs__content {
      padding: 0 20px 20px;
      .tabs-btn {
        text-align: center;
        margin-top: 20px;
        button {
          // width: 100px;
          height: 36px;
          padding: 0 35px;
          border-radius: 0;
          outline: none;
          border: 1px solid rgb(233, 233, 233);
          cursor: pointer;
          background: #fff;
          &:hover {
            color: #fff;
            background: #3361ea;
          }
          &:nth-of-type(1) {
            border-right: 0;
            border-top-left-radius: 5px;
            border-bottom-left-radius: 5px;
          }
          &:nth-of-type(2) {
            border-left: 0;
            border-top-right-radius: 5px;
            border-bottom-right-radius: 5px;
          }
        }
        button.active {
          color: #0088cc;
          background: rgba(32, 108, 231, 0.1);
          border: 1px solid #0088cc;
          &:hover {
            color: #fff;
            background: #3361ea;
          }
        }
      }
      .tabs-article {
        text-align: center;
        padding: 60px 20px;
        img {
          margin-bottom: 20px;
        }
        p {
          color: #999;
          font-size: 14px;
        }
      }
      .toLoad {
        width: 100%;
        p {
          width: 266px;
          height: 36px;
          line-height: 36px;
          margin: 0 auto;
          font-size: 14px;
          text-align: center;
          color: #08c;
          border: 1px solid #08c;
          border-radius: 4px;
          box-sizing: border-box;
          cursor: pointer;
          &:hover {
            color: #fff;
            background: #3361ea;
            border: 1px solid #3361ea;
          }
        }
      }
    }
  }
  /deep/.el-tabs__nav-scroll {
    display: flex;
    justify-content: center;
    height: 50px;
    line-height: 50px;
    .el-tabs__item {
      padding: 0 !important;
      margin: 0 30px;
    }
    .el-tabs__item.is-active {
      color: #08c;
      border-bottom: 2px solid #0088cc !important;
    }
  }
  /deep/.el-tabs--card > .el-tabs__header .el-tabs__item {
    border: 0;
    height: 44px;
  }
  /deep/.el-tabs--card > .el-tabs__header .el-tabs__nav {
    border: 0;
  }
}
</style>
